<%@ page contentType="text/html;charset=UTF-8" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@include file="../../common/operation_forbid.jsp" %>
<!DOCTYPE html >
<html lang="en">
<head>
<%@include file="../../common/includeBaseSet.jsp" %>
<%@include file="../../common/includeSystemSet.jsp" %>
<script type="text/javascript" src="${jypath}/static/js/jquery/ajaxfileupload.js" ></script>
<script type="text/javascript">
	function importFile(){
		var file = $("#multipartFile").val();
		if(file==null||file==""){
			layer.alert("请选择上传EXCEL文件");
		}else{
			 var pos = file.lastIndexOf(".");  
			 var lastname = file.substring(pos,file.length);  
			 var resultName=lastname.toLowerCase(); 
			 if ('.xls'!=resultName.toString()&&'.xlsx'!=resultName.toString()){
				 layer.alert("请上传.xls或.xlsx后缀的文件!");
			 }else{
					var id=$("input[name='id']").val();
					var type=$("#house_type").val();
					$.ajaxFileUpload({
						type: "POST",
						url:"${jypath}/estate/import/importExcel?id="+id+"&type="+type,
						fileElementId:'multipartFile',
						dataType: "json",
						success: function(obj) {
							$("input[name='batchCode']").val(obj.result);
							if(obj.total && obj.st && obj.et){
								setTimeout(function(){
									layer.alert("导入:"+obj.total+"条,成功:"+obj.st+"条,失败:"+obj.et+"条");
								}, 3000) ;
							}
							getList();
					 }
					});
			 }
		}
	
	}
	
	function getList(){
	
		var batchNo=$("input[name='batchCode']").val();
		JY.Ajax.doRequest("searchForm",jypath +'/estate/importlog/list',null,function(data){
			
			 //数据集合
			   $("#baseTable tbody").empty();
			  //分页对象
	         var page=data.obj;
	    	 var results=page.rows;
	    	 //分页数据
	     	 var pageNum=page.pageNumber,pageSize=page.pageSize,totalRecord=page.total;
	    	 var html="";
			 if(results!=null&&results.length>0){
	    		 for(var i = 0;i<results.length;i++){	 
	    			 //导入结果
	    			 var l=results[i];
	    			 var resultName="";
	    			 if(l.result=="0"){
	    				 resultName="失败";
	    			 }else{
	    				 resultName="成功";
	    			 }
	    			 html+="<tr>";
	    			 html+="<td class='center'><label> <input type='checkbox' name='ids' value='"+l.id+"' class='ace' /> <span class='lbl'></span></label></td>";
	    			 html+="<td class='center'>"+JY.Object.notEmpty(l.title)+"</td>";
	    			
	    			 html+="<td class='center'>"+JY.Object.notEmpty(resultName)+"</td>";
	    			 html+="<td class='center'>"+JY.Object.notEmpty(l.remark)+"</td>";
	    			 html+="<td class='center'>"+JY.Object.notEmpty(new Date(l.createTime).Format("yyyy-MM-dd"))+"</td>";
	    			 html+="</tr>";	
	    		 }
	    		 $("#baseTable tbody").append(html);
	    		 JY.Page.setPage("searchForm","pageing",pageSize,pageNum,totalRecord,"getList");
	    		
			 }else{
	     		html+="<tr><td colspan='10' class='center'>没有相关数据</td></tr>";
	    		$("#baseTable tbody").append(html);
	    		$("#pageing ul").empty();//清空分页
	    	 }
			 JY.Model.loadingClose();
		}); 
	}

	
	
	//日期格式化
	Date.prototype.Format = function (fmt) { 
	    var o = {
	        "M+": this.getMonth() + 1, //月份 
	        "d+": this.getDate(), //日 
	        "h+": this.getHours(), //小时 
	        "m+": this.getMinutes(), //分 
	        "s+": this.getSeconds(), //秒 
	        "q+": Math.floor((this.getMonth() + 3) / 3), //季度 
	        "S": this.getMilliseconds() //毫秒 
	    };
	    if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
	    for (var k in o)
	    if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
	    return fmt;
	} 
	
	function toBackProjectDetail(){
	    var projectId=$("#projectId").val();
		var tabKey = "batchImp_" + projectId;
		parent.TabControlRemove(tabKey);
	}
	
	function downLoadTempSet(){
		var url_gy="${fatTemplet}";
		var url_bx="${villaTemplet}";
		var url_cw="${parkingTemplet}";
        url_gy = url_gy.replace("gy_templet.xls", "gy_templet_lc.xls");
        url_gy = url_gy.replace("bx_templet.xls", "bx_templet_lc.xls");
        url_gy = url_gy.replace("cw_templet.xls", "cw_templet_lc.xls");
		var house_type=$("#house_type").val();
		if(house_type=="0"){
			$("#templet_download_url").attr("href",url_gy);
		}else if(house_type=="1"){
			$("#templet_download_url").attr("href",url_bx);
		}else if(house_type=="2"){
			$("#templet_download_url").attr("href",url_cw);
		}
	}
	
</script>
</head>
<body>	
 	<form id="my_import">
		<table class="table table-striped table-bordered table-hover">
			<tr>
			<td>项目名称:</td>
			<td>${project.projectName}</td>
			<td>导入对象:</td>
			<td>
			<select name="type" id="house_type" onchange="downLoadTempSet()">
				<option value="0">住宅</option>
				<option value="1">商业</option>
				<option value="2">配套</option>
			</select>
			</td>
			<td>选择文件:</td>
			<td><input type="file" name="multipartFile" id="multipartFile" enctype="multipart/form-data"></td>
			<td>
			<button type="button" onclick="importFile()" id="import_button" class="btn btn-info btn-sm"><span class="glyphicon glyphicon-plus">导入</span></button>
			<button class="btn btn-info btn-sm" onclick="toBackProjectDetail();">返回</button>	
			<a href="${fatTemplet}" id="templet_download_url">模板下载</a>
			
			</td>
			</tr>
		</table>
	    <input type="hidden" name="id" value="${project.id}" id="projectId">
	    <input type="hidden"  value="${menu}" id="menu">
	    
	</form> 
	<form id="searchForm" class="form-inline" method="POST" onsubmit="return false;">
		<input type="hidden" class='pageNum' name='pageNum' value='1'/>
						<input type="hidden" class='pageSize'  name='pageSize' value='10'/>
						<input type="hidden"   name='batchCode' />
	</form>
	
	<table id="baseTable" class="table table-striped table-bordered table-hover" >
						<thead>
							<tr>
								<th style="width:1%" class="center">
									<label><input type="checkbox" class="ace" ><span class="lbl"></span></label>	
								</th>
								<th  class="center">房间</th>
								<th  class="center">导入结果</th>
								<th  class="center">原因</th>
	                            <th  class="center">日期</th>
							</tr>
						</thead>
						<tbody></tbody>
					</table>
						<div class="row">
						<div class="col-sm-8">
							<!--设置分页位置-->
							<div id="pageing" class="dataTables_paginate paging_bootstrap">
								<ul class="pagination"></ul>
							</div>
						</div>
					</div>
					<%@include file="../../common/dialog.jsp" %>	
				
</body>
</html>